
'use strict'

const IORedis = require('ioredis')

const nodes = []
for (const address of process.env.REDIS_ADDRESSES.split(',')) {
  const addr = address.split(':')
  if (!Array.isArray(addr) || addr.length !== 2) {
    throw Error('process.env.REDIS_ADDRESSES error!')
  }

  nodes.push({
    port: addr[1],
    host: addr[0]
  })
}

const RedisCluster = new IORedis.Cluster(nodes, {
  redisOptions: {
    password: process.env.REDIS_PASSWORD
  }
})

const Redis = new IORedis(nodes[0].port, nodes[0].host, {
  password: process.env.REDIS_PASSWORD
})

module.exports = {
  Redis,
  RedisCluster
}
